﻿'use strict';


var app = angular.module('app', [
        // Angular modules 
        'ngAnimate',        // animations
        'ngRoute',          // routing
        'ngSanitize',       // sanitizes html bindings (ex: sidebar.js)

        // Custom modules 
        //'common',           // common functions, logger, spinner
        //'common.bootstrap', // bootstrap dialog wrapper functions        

        // 3rd Party Modules
        'ui.bootstrap',      // ui-bootstrap (ex: carousel, pagination, dialog)
        'LocalStorageModule',
        'ui.select'
]);

app.config(function ($routeProvider) {

    $routeProvider.when("/home", {
        controller: "homeController",
        templateUrl: "/app/views/home.html"
    });

    $routeProvider.when("/login", {
        controller: "loginController",
        templateUrl: "/app/views/login.html"
    });

    $routeProvider.when("/signup", {
        controller: "signupController",
        templateUrl: "/app/views/signup.html"
    });

    $routeProvider.when("/notauthorized", {
        controller: "notauthorizedController",
        templateUrl: "/app/views/notauthorized.html"
    }); 
   /* $routeProvider.when("/orders", {
        controller: "ordersController",
        templateUrl: "/app/views/orders.html",
        resolve: {
            checkSecurity: checkSecurity,
        },
        settings: {
            loginRequired: true,
            roles: ['User'],
        }
    });*/

    $routeProvider.when("/base", {
        //controller: "base1Controller",
        templateUrl: "/app/views/base1.html"
    });

    $routeProvider.when("/proveedor", {
        //controller: "proveedorController",
        templateUrl: "/app/views/proveedor.html"
    });
    $routeProvider.when("/referenciacomprobante", {
        //controller: "referenciaComprobanteController",
        templateUrl: "/app/views/referenciaComprobante.html"
    });
    $routeProvider.when("/elemento", {
        //controller: "elementoController",
        templateUrl: "/app/views/elemento.html"
    });
    $routeProvider.when("/tabla2", {
        //controller: "tabla2Controller",
        templateUrl: "/app/views/tabla2.html"
    });
    $routeProvider.when("/tabla10", {
        //controller: "tabla10Controller",
        templateUrl: "/app/views/tabla10.html"
    });
    $routeProvider.when("/baseImponible", {
        //controller: "baseImponibleController",
        templateUrl: "/app/views/baseImponible.html"
    });

    $routeProvider.when("/reporteCompras", {
        templateUrl: "/app/views/reporteCompras.html"
    });
    $routeProvider.when("/reporteLibroDiario", {
        templateUrl: "/app/views/reporteDiario.html"
    });
    $routeProvider.otherwise({ redirectTo: "/home" });

});





checkSecurity.$inject = ['$route', '$q', '$location', 'authService'];
function checkSecurity($route, $q ,$location, authService) {
        var deferred = $q.defer();
        authService.fillAuthData().then(function () {
            var settings = $route.current.settings;
            var loginRequired = settings.loginRequired || false;
            var roles = settings.roles || [];
            if (loginRequired) {
                if (!authService.authentication.isAuth) {
                    $location.path('/login');
                } else {
                    if (roles.length > 0) {
                        if (!checkRole(authService.authentication.roles, roles)) {
                            $location.path('/notauthorized').replace();
                        }
                    }
                }
            }
            deferred.resolve(true); //We want to return just true even if we have to re-route. 
                                    //If we returned an reject, the the global handler will re-route us to home
        }, function(error) {
            deferred.reject(error);
        });

        return deferred.promise;               
    }

function checkRole(userRoles, rolesToCheck) {
    if (rolesToCheck.length == 0) {
        return true;
    }
    if (userRoles.length == 0) {
        return false;
    }
    for (var i = 0; i < userRoles.length; i++) {
        if (rolesToCheck.indexOf(userRoles[i]) > -1) {
            return true;
        }
    }
    return false;
}
function checkEstado(userEstado, estadoToCheck) {
    if (estadoToCheck.length == 0) {
        return true;
    }
    if (userEstado.length == 0) {
        return false;
    }
    for (var i = 0; i < userEstado.length; i++) {
        if (estadoToCheck.indexOf(userEstado[i]) > -1) {
            return true;
        }
    }
    return false;
}
